/*
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


#include <sourcemod>
#include <sdktools>
#include <l4d_stocks>
#include <l4d_weapon_stocks>

#include "macros.sp"
#include "helpers.sp"


//cvar
new Handle:time_update = INVALID_HANDLE;
new Handle:announce = INVALID_HANDLE;
new Handle:cooldown = INVALID_HANDLE;
new Handle:redrug = INVALID_HANDLE;
new Handle:max_angle = INVALID_HANDLE;

//variables
new bool:players_under_drug[MAXCLIENTS] = { false, ... };
new Handle:timers_drug[MAXCLIENTS];
new bool:can_drug[MAXCLIENTS] = { true, ...}; //controled by cooldown


public OnPluginStart()
{

	drug_init();

	time_update = CreateConVar("l4d2_spitterdrug_time_update", "15", "Time before each drug update");
	announce = CreateConVar("l4d2_spitterdrug_announce", "1", "Should announce when players are drug ?");
	cooldown = CreateConVar("l4d2_spitterdrug_cooldown", "5", "How many time before spitter can re-drug");
	redrug = CreateConVar("l4d2_spitterdrug_redrug", "1", "Can the spitter re-drug the same player before the end of previous drug ?");
	max_angle = CreateConVar("l4d2_spitterdrug_angle", "70", "Max angle to rotate camera"); 
	AutoExecConfig(true, "l4d2_spitterdrug");

	HookEvent("round_start", Event_OnRoundStart);	
	HookEvent("player_hurt", Event_PlayerHurt);
	HookEvent("heal_success", Event_HealSuccess);
	HookEvent("revive_success", Event_ReviveSuccess, EventHookMode_Post);
}

public Event_OnRoundStart(Handle:event, const String:name[], bool:dontBroadcast)
{
	clear();
}

public Event_ReviveSuccess(Handle:event, const String:name[], bool:dontBroadcast)
{
	new userid = GetClientOfUserId(GetEventInt(event, "subject"));
	remove_drug(userid);
}

public Event_HealSuccess(Handle:event, const String:name[], bool:dontBroadcast)
{
	new userid = GetClientOfUserId(GetEventInt(event, "subject"));
	remove_drug(userid);
	
}

public Event_PlayerHurt(Handle:event, const String:name[], bool:dontBroadcast)
{
	new userid = GetClientOfUserId(GetEventInt(event, "userid"));
	new attackid = GetClientOfUserId(GetEventInt(event, "attacker"));

	if(IsValidHumanSurvivorAlive(userid) && IsValidInfected(attackid))
	{

		decl String:wep[32];
		GetEventString(event, "weapon", wep, sizeof(wep));
		
		if(strcmp(wep, "spitter_claw") == 0) //the spitter claw weapon
		{

			if(can_drug[attackid]) //cooldown elapsed
			{

				if(players_under_drug[userid]) //already under drug
				{
					if(GetConVarInt(redrug) == 1) //can redrug 
					{					
						KillDrug(userid);
					}
					else
					{
						return;
					}
				}

				can_drug[attackid] = false;
				CreateTimer(GetConVarFloat(cooldown), Timer_CanDrug, attackid, TIMER_FLAG_NO_MAPCHANGE);
				players_under_drug[userid] = CreateDrug(userid, GetConVarFloat(max_angle));
				if(players_under_drug[userid])
				{
					timers_drug[userid] = CreateTimer(GetConVarFloat(time_update), Stop_Timer_Drug, userid, TIMER_FLAG_NO_MAPCHANGE);
				}
				if(GetConVarInt(announce) == 1)
				{
					PrintToChatAllExclude(userid, attackid, "\x04%N\x01 was \x02Spit Drugged\x01 by \x04%N\x01", userid, attackid);
					PrintToChatOnly(userid, attackid, "\x05%N\x01 was \x02Spit Drugged\x01 by \x05%N\x01", userid, attackid);
				}
			}
		}
	}

	return;
}

clear()
{
	for(new i=0; i < MAXCLIENTS; i++)
	{
		players_under_drug[i] = false;
		remove_drug(i);
		can_drug[i] = true;
	}
}


public Action:Stop_Timer_Drug(Handle:timer, any:client)
{
	remove_drug(client);
}

public Action:Timer_CanDrug(Handle:timer, any:client)
{
	can_drug[client] = true;
}


remove_drug(client)
{
	players_under_drug[client] = !KillDrug(client);

}